package com.youhong.teethcare.customviews

import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Canvas
import android.util.AttributeSet
import android.view.View
import com.youhong.teethcare.R
import java.util.*

class BrushAreaShowView : View {
    var bitmapBackground: Bitmap? = null
    var areaList: List<Int> = ArrayList()

    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {
        init(context)
    }

    fun init(context: Context?) {
        bitmapBackground = BitmapFactory.decodeResource(resources, R.drawable.brush_history_bg)
    }

    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {}

    fun updateAreaList(areaList: List<Int>) {
        this.areaList = areaList
        postInvalidate()
    }
    fun updateBg(bitmap: Bitmap?) {
        this.bitmapBackground = bitmap
        postInvalidate()
    }
    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        drawBackground(canvas)
        drawArea(canvas)
    }

    private fun drawArea(canvas: Canvas) {
        for (id in areaList) {
            val bitmapArea = BitmapFactory.decodeResource(resources, id)
            drawBitmap(canvas, bitmapArea)
        }
    }

    fun drawBackground(canvas: Canvas) {
        drawBitmap(canvas, bitmapBackground)
    }

    fun drawBitmap(canvas: Canvas, bitmap: Bitmap?) {
        val left = 0
        val top = 0
        canvas.drawBitmap(bitmap!!, left.toFloat(), top.toFloat(), null)
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        val backWidth = bitmapBackground!!.width
        val backHeight = bitmapBackground!!.height
        setMeasuredDimension(backWidth, backHeight)
    }


}